/*
 2011 (C) Stefan Grufman
 MyPilot

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 any later version. see <http://www.gnu.org/licenses/>
 */
#include "defines.h"
#include <avr/eeprom.h>

static const uint16_t eeprom_data_version = (0x0001 | (PARAM_COUNT<<8));

struct params_struct
{
	float param[PARAM_COUNT];
	char param_name[PARAM_COUNT][PARAM_NAME_LENGTH];
};

struct params_struct params_data;

void params_setup()
{
	params_read_from_eeprom();
}

inline float params_get(unsigned int p)
{
	return params_data.param[p];
}

inline char* params_get_name(unsigned int p)
{
	return params_data.param_name[p];
}

inline unsigned int params_find_param(char* name)
{
	for (int i=0; i<PARAM_COUNT; i++)
	{
		if (strcmp(name, params_data.param_name[i]) == 0)
		{
			return i;
		}
	}
	return PARAM_COUNT;
}

inline void params_set(unsigned int p, float value)
{
	params_data.param[p] = value;
	params_save_single_to_eeprom(p);
}

static inline void params_reset_defaults(void)
{
	params_data.param[PARAM_EEPROM_VER] = (float)eeprom_data_version;
	strcpy(params_data.param_name[PARAM_EEPROM_VER], PARAM_EEPROM_VER_NAME);

	params_data.param[PARAM_SYSTEM_ID] = USR_SYSTEM_ID;
	strcpy(params_data.param_name[PARAM_SYSTEM_ID], PARAM_SYSTEM_ID_NAME);

	params_data.param[PARAM_COMPONENT_ID] = USR_COMPONENT_ID;
	strcpy(params_data.param_name[PARAM_COMPONENT_ID], PARAM_COMPONENT_ID_NAME);

	params_data.param[PARAM_FRAME_TYPE] = USR_FRAME_TYPE;
	strcpy(params_data.param_name[PARAM_FRAME_TYPE], PARAM_FRAME_TYPE_NAME);

}

inline void params_save_to_eeprom(void)
{
	unsigned int eeprom_address = 0;

	// Write EEPROM version
	eeprom_write_block((void*)(&eeprom_data_version), (void*)(eeprom_address), sizeof(eeprom_data_version));
	eeprom_address += sizeof(eeprom_data_version);

	// Write the params
	for (int i=0; i<PARAM_COUNT; i++)
	{
		eeprom_write_block((void*)(&params_data.param[i]), (void*)(eeprom_address), sizeof(float));
		eeprom_address += sizeof(float);
	}
}

inline void params_save_single_to_eeprom(unsigned int p)
{
	unsigned int eeprom_address = 0;

	eeprom_address += sizeof(eeprom_data_version) + sizeof(float)*p;

	// Write the param
	eeprom_write_block((void*)(&params_data.param[p]), (void*)(eeprom_address), sizeof(float));
	eeprom_address += sizeof(float);
}

inline void params_read_from_eeprom(void)
{
	unsigned int eeprom_address = 0;
	uint16_t test_val;

	// Read EEPROM version
	eeprom_read_block((void*)&test_val, (void*)(eeprom_address), sizeof(test_val));
	eeprom_address += sizeof(eeprom_data_version);

	if (test_val == eeprom_data_version)
	{
		// Call this to "set names"
		params_reset_defaults();

		// Value was OK, let's read values from EEPROM
		for (int i=0; i<PARAM_COUNT; i++)
		{
			eeprom_read_block((void*)(&params_data.param[i]), (void*)(eeprom_address), sizeof(float));
			eeprom_address += sizeof(float);
		}
	}
	else
	{
		// Not OK...so let's restore values to default and save them
		params_reset_defaults();
		params_save_to_eeprom();
	}
}


/*void params_loop(unsigned long current_ms)
{

}*/
